/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package post.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import post.Server;

/**
 *
 * @author Stephen
 */
public class ServerUI {
    
    private Server server;
    
    public ServerUI() {
        server = new Server();
        server.init();
    }
    
    /**
     * This method displays a command menu
     * @return is a string containing the command
     */
    public String showMenu() {
        String cmd = "";
        try {
            System.out.println("Commands");
            System.out.println("1 - Add Product");
            System.out.println("2 - View transactions by date");
            System.out.println("3 - View transactions by customer");
            System.out.println("4 - Close store");
            System.out.println("Please enter command number: ");
            
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            cmd = br.readLine();
        } catch(Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
        return cmd;
    }
    
    /**
     * This method executes a command
     * @param cmd is the command to be executed
     */
    public void processCommand(String cmd) {
        switch (Integer.parseInt(cmd)) {
            case 1:
                try {
                    String upc, desc;
                    float price;
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Enter UPC: ");
                    upc = br.readLine();
                    System.out.println("Enter Description: ");
                    desc = br.readLine();
                    System.out.println("Enter Price: ");
                    price = Float.parseFloat(br.readLine());
                    server.addProductToCatalog(upc, desc, price);
                } catch (Exception e) {
                    System.err.println("ERROR: " + e.toString());
                }
                break;
                
            case 2:
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Enter Date in mm/dd/yyyy format: ");
                    Date date = new SimpleDateFormat("MM/dd/yyyy").parse(br.readLine());
                    String[] trans = server.getTransactionsbyDate(date);
                    System.out.println();
                    for (int i = 0; i < trans.length; i++) {
                        System.out.println(trans[i]);
                    }
                    System.out.println();
                } catch (Exception e) {
                    System.err.println("ERROR: " + e.toString());
                }
                break;
                
            case 3:
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Enter customer name ");
                    String name = br.readLine();
                    String[] trans = server.getTransactionsByCustomer(name);
                    System.err.println();
                    for (int i = 0; i < trans.length; i++) {
                        System.out.println(trans[i]);
                    }
                    System.out.println();
                } catch (Exception e) {
                    System.err.println("ERROR: " + e.toString());
                }
                break;
                
            case 4:
                System.out.println("Closing store...");
                System.exit(0);
                
            default:
                System.out.println("Invalid Command");
                System.out.println();
        }
    }
    
    public static void main(String args[]) {
        
        ServerUI ui = new ServerUI();
        
        while (true) {
            ui.processCommand(ui.showMenu());
        }
        
    }
}
